package health.lm.com.component_base.base.application;

import android.app.Activity;
import android.app.Application;
import android.app.Service;
import android.content.Context;
import android.os.Vibrator;
import android.support.multidex.MultiDex;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.google.gson.GsonBuilder;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;
import com.vondear.rxtool.RxTool;

import cn.bluemobi.dylan.step.step.service.StepService;
//import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.JPushInterface;
import health.lm.com.component_base.BuildConfig;
import health.lm.com.component_base.R;
import health.lm.com.component_base.util.ActivityControl;
import health.lm.com.data.App;
import health.lm.com.data.HttpCST;
import health.lm.com.network.EasyHttp;
import health.lm.com.network.callback.FriendsMsgCallBack;
import health.lm.com.network.model.ApiResult;
import health.lm.com.router.ARouterConstant;
import health.lm.com.router.RouterConfig;

public class BaseApplication extends App {
    public static final String TAG = "BaseApp";
    private static BaseApplication mContext;

    public Vibrator mVibrator;
    private ActivityControl mActivityControl;
    public StepService stepService;


    /**
     * 获取Application
     *
     * @return BiliCopyApplication
     */
    public static BaseApplication getInstance() {
        return mContext;
    }


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        //MultiDex分包方法 必须最先初始化
        MultiDex.install(this);
    }

    public void addActivity(Activity activity) {
        mActivityControl.addActivity(activity);
    }

    /**
     * 移除
     */
    public void removeActivity(Activity activity) {
        mActivityControl.removeActivity(activity);
    }


    public void exit() {
        mActivityControl.appExit();
    }

    public static final String AppId = "wx28b69149f613318f";
    @Override
    public void onCreate() {
        super.onCreate();
        ZXingLibrary.initDisplayOpinion(this);
        RxTool.init(this);
        mActivityControl = new ActivityControl();
        mContext = this;
        //arouter路由初始化
        RouterConfig.init(this, BuildConfig.DEBUG);
        mVibrator = (Vibrator) getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);
        Utils.init(this);
        EasyHttp.init(this);
        EasyHttp.getInstance().setBaseUrl(HttpCST.BASE_URL).setEncrypt(true);
        EasyHttp.getInstance().addFriendsMsg(new FriendsMsgCallBack() {
            @Override
            public void friendsMas(ApiResult apiResult) {
                LogUtils.d("请求结果 ------------- \n"
                        , new GsonBuilder().create().toJson(apiResult));
                int code = apiResult.getResult_code();
                switch (code) {
                    //请求成功
                    case 0:
                        if (apiResult.getResult_data() == null) {
                            if(!StringUtils.isEmpty(apiResult.getResult_info())) {
                                ToastUtils.showShort(apiResult.getResult_info());
                            }
                        }
                        break;
                    //登录异常
                    case 1111:
                        ToastUtils.showShort(apiResult.getResult_info());
                        //清空用户信息
                        SPUtils.getInstance("base").put("uid", "");
                        SPUtils.getInstance("base").put("access_token", "");
                        App.getModel().setAccess_token("");
                        ARouter.getInstance().build(ARouterConstant.LoginActivity)
                                .withString("type", "login")
                                .navigation();
                        break;
                    //用户输入错误
                    case 2222:
                        ToastUtils.showShort(apiResult.getResult_info());
                        break;
                    //APP端开发者请求错误（接口字段缺失、数据格式错误等）
                    case 3333:
                        ToastUtils.showShort("开发小哥正在路上");
                        break;
                    //服务器端异常
                    case 4444:
                        ToastUtils.showShort("服务器走丢了……");
                        break;
                    default:
                        break;
                }
            }
        });

        //testData();

        //极光推送
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
        /**
         * 设置组件化的Log开关
         * 参数: boolean 默认为false，如需查看LOG设置为true
         */
        UMConfigure.setLogEnabled(true);
        /**
         * 初始化common库
         * 参数1:上下文，不能为空
         * 参数2:【友盟+】 AppKey
         * 参数3:【友盟+】 Channel
         * 参数4:设备类型，UMConfigure.DEVICE_TYPE_PHONE为手机、UMConfigure.DEVICE_TYPE_BOX为盒子，默认为手机
         * 参数5:Push推送业务的secret
         */
        UMConfigure.init(this,"5d42b04e0cafb23bd300002a","umeng",UMConfigure.DEVICE_TYPE_PHONE,"");
    }
    {
        PlatformConfig.setWeixin(AppId, "a23383912854418fcc38c83346ac6d70");
        PlatformConfig.setQQZone("101540888", "c7394704798a158208a74ab60104f0ba");
    }

    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                //全局设置主题颜色
                layout.setPrimaryColorsId(R.color.colorPrimary, R.color.color_83d6aa);
                //.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
                return new ClassicsHeader(context);
            }
        });
//        //设置全局的Footer构建器
//        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
//            @Override
//            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
//                //指定为经典Footer，默认是 BallPulseFooter
//                return new ClassicsFooter(context).setDrawableSize(20);
//            }
//        });
    }
}